<template>
  <div class="translation-manager">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>翻译词条管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            添加词条
          </el-button>
        </div>
      </template>

      <el-table :data="translations" style="width: 100%" v-loading="loading">
        <el-table-column prop="key" label="词条键" width="200" />
        <el-table-column prop="english" label="英文原文" />
        <el-table-column prop="chinese" label="中文翻译">
          <template #default="{ row }">
            <el-input v-model="row.chinese" @blur="handleUpdate(row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加词条对话框 -->
    <el-dialog v-model="dialogVisible" title="添加词条" width="500px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="词条键">
          <el-input v-model="form.key" />
        </el-form-item>
        <el-form-item label="英文原文">
          <el-input v-model="form.english" type="textarea" />
        </el-form-item>
        <el-form-item label="中文翻译">
          <el-input v-model="form.chinese" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitAdd">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'

interface Translation {
  id?: number
  key: string
  english: string
  chinese: string
}

const translations = ref<Translation[]>([])
const loading = ref(false)
const dialogVisible = ref(false)
const form = ref<Translation>({
  key: '',
  english: '',
  chinese: ''
})

const fetchTranslations = async () => {
  loading.value = true
  try {
    const response = await axios.get('http://localhost:8000/api/translations')
    translations.value = response.data
  } catch (error) {
    console.error('获取翻译失败:', error)
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  form.value = { key: '', english: '', chinese: '' }
  dialogVisible.value = true
}

const submitAdd = async () => {
  try {
    await axios.post('http://localhost:8000/api/translations', form.value)
    dialogVisible.value = false
    fetchTranslations()
  } catch (error) {
    console.error('添加翻译失败:', error)
  }
}

const handleUpdate = async (row: Translation) => {
  try {
    await axios.put(`http://localhost:8000/api/translations/${row.id}`, row)
  } catch (error) {
    console.error('更新翻译失败:', error)
  }
}

const handleDelete = async (row: Translation) => {
  try {
    await axios.delete(`http://localhost:8000/api/translations/${row.id}`)
    fetchTranslations()
  } catch (error) {
    console.error('删除翻译失败:', error)
  }
}

onMounted(() => {
  fetchTranslations()
})
</script>

<style scoped>
.translation-manager {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
